package fr.zadoka.zadokadna;

import android.app.Activity;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ZadokaNavigationDrawer {

	private ActionBarActivity mOwner;
	private String[] mMenuTitles;
    private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ZadokaDrawerToggle mDrawerToggle;
	
	public enum ZadokaMenuEntry { Daily, Favorite, Settings };
    
    public DrawerLayout getDrawerLayout() {
		return mDrawerLayout;
	}


	public ZadokaNavigationDrawer(ActionBarActivity owner)
	{	
		mOwner = owner;
	    mMenuTitles = mOwner.getResources().getStringArray(R.array.menu_items_list);
        mDrawerLayout = (DrawerLayout) mOwner.findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) mOwner.findViewById(R.id.left_drawer);

    	// set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);        
        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(owner, R.layout.drawer_list_item, mMenuTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        
        mDrawerToggle = new ZadokaDrawerToggle(mOwner, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        
        mOwner.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mOwner.getSupportActionBar().setHomeButtonEnabled(true);
        mOwner.getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
        
	}
	
	public Boolean isOpen()
	{
		return mDrawerLayout.isDrawerOpen(mDrawerList);
	}
	
	private class DrawerItemClickListener implements ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			selectItem(position);
			
		}
	}
	
	private class ZadokaDrawerToggle extends ActionBarDrawerToggle
	{

		public ZadokaDrawerToggle(Activity activity, DrawerLayout drawerLayout,
				int drawerImageRes, int openDrawerContentDescRes,
				int closeDrawerContentDescRes) {
			super(activity, drawerLayout, drawerImageRes, openDrawerContentDescRes,
					closeDrawerContentDescRes);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onDrawerClosed(View drawerView) {
			// TODO Auto-generated method stub
			super.onDrawerClosed(drawerView);
			mOwner.supportInvalidateOptionsMenu();
		}

		@Override
		public void onDrawerOpened(View drawerView) {
			// TODO Auto-generated method stub
			super.onDrawerOpened(drawerView);
			mOwner.supportInvalidateOptionsMenu();
		}
		
	}	


	/** Swaps fragments in the main content view */
	private void selectItem(int position) {	
		switch(position)
		{
		case 0 :
			ActivitySwitcher.switchTo(mOwner, ZadokaMenuEntry.Daily);			
			break;
		case 1 :
			ActivitySwitcher.switchTo(mOwner, ZadokaMenuEntry.Favorite);			
			break;
		case 2 :
		default:
			break;
		}
	    // Highlight the selected item, update the title, and close the drawer
	    mDrawerList.setItemChecked(position, true);
	    mDrawerLayout.closeDrawer(mDrawerList);
	}


	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return mDrawerToggle.onOptionsItemSelected(item);
	}	
	
}
